<%@page import="Technical.Database"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="Domain.*" %>


<jsp:useBean id="currentUser" scope="session" class="Domain.UserBean" />

<% 
    Website web = Website.getInstance();
    BlogBean blog;
    
    if(request.getParameter("blog") == null){
        blog = web.getBlog(1);
    }
    else{
        String s = (String) request.getParameter("blog");
        int id = Integer.parseInt(s);
        blog = web.getBlog(id);
    }
    PostBean post;
    if(request.getParameter("post") == null)
        post = blog.getLatestPost();
    else{
        String s = (String) request.getParameter("post");
        int id = Integer.parseInt(s);
        if(blog.postExists(id)){
            post = blog.getPost(id);
        }else
            post = blog.getLatestPost();
    }
    
    Boolean loggedIn = false;
    if(currentUser.validatePassword()) {
        loggedIn = true;
}%>

<!DOCTYPE html>
<html>
    <head>
		<title>Blogs 'R' Us</title>
		<link rel="stylesheet" type="text/css" href="stylesheets/general.css" />
	</head>
    <body>
        <div class = "header">
            <div id="headerName"><h1 id ="margin"><% out.print(blog.getblogName()); %></h1></div>
            <% if(loggedIn){ %>
                <div id ="headerLogin">Logged in as:
                    <%out.print(currentUser.getuserName());%>
                    <a href="login.jsp?login=logout">
                        <input type="submit" value="Logout" />
                    </a>
                    <a href="index.jsp?page=createNewPost">
                        <input type="submit" value="Create New Post" />
                    </a>
                </div>
            <% }else if(currentUser.getuserName() == null){ %>
                    <jsp:include page="login.jsp"/>
                    <a href="index.jsp?page=register">
                        <input type="submit" value="Register" />
                    </a>
            <% }else{ %>
                    <jsp:include page="login.jsp?login=badlogin"/>
                    <a href="index.jsp?page=register">
                        <input type="submit" value="Register" />
                    </a>
            <% }%>
            
		</div>
		<div class = "main">
            <h2 id ="margin"><% out.print(post.getHeader()); %> </h1>
            <h3 id ="margin"><% out.print(post.getAuthor().getuserName()); %></h3>
            <p id ="margin"><% out.print(post.getText());%></p>
            <hr>
            <%
            Iterator it = post.getAllComments();
            while(it.hasNext()){
                CommentBean c = (CommentBean) it.next();%>
                <h3 id="margin"><%out.print(c.getAuthorName());%>
                <%out.print(c.getdate());%></h3>
                <p id="margin"><%out.print(c.gettext());%></p>
                <hr>
            <%}
            if(loggedIn){%>
                <jsp:useBean id="comment" class="Domain.CommentBean" scope="page"/>
                <jsp:setProperty name="comment" property="*"/>

                <form id="margin" method="POST" action="blogview.jsp">
                    Comment:<br/><textarea name="text" rows="5" cols="60"></textarea><br/>
                    <input type="submit" value="Send"/>
                </form>
                <%
                if(comment.gettext() != null){
                    comment.setuser(currentUser);
              
                    post.addComment(comment);
                    response.sendRedirect("blogview.jsp");
                }
            }
            %>
        </div>
        <% String path = "sidebar.jsp?blog=" + blog.getblogId(); %>
        <jsp:include page= "<%= path %>"/>
    </body>
</html>